package cn.gaotu.basic.controller;

import cn.gaotu.basic.dto.LoginDto;
import cn.gaotu.basic.dto.WechatDto;
import cn.gaotu.basic.excepion.BusinessException;
import cn.gaotu.basic.util.AjaxResult;
import cn.gaotu.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RequestMapping("/login")
@RestController
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto dto) {
        try {
            Map<String, Object> map = logininfoService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) { //捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!");
        }

    }

    /**
     * 微信扫码登录
     * @param dto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto dto) {
        try {
            return logininfoService.wechat(dto);
        } catch (BusinessException e) { //捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!");
        }
    }
    /**
     * 微信扫码绑定
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto dto) {
        try {
            Map<String, Object> map = logininfoService.binder(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) { //捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!");
        }
    }


}

